﻿
namespace Data.Repository
{
	using System.Linq;
	using System.Collections.Generic;

	public class UserExtensionRepository : Data.Repository.IUserExtensionRepository
	{
		private LinqToECommerceDBDataContext _dbContext;

		#region IUserExtensionRepository Members

		public int CreateNewUserExtension(Data.Entity.UserExtension theNewUserExtension)
		{
			if(theNewUserExtension == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_UserExtension_Create(theNewUserExtension.UserID, theNewUserExtension.AffiliateID, theNewUserExtension.BillingAddressID, theNewUserExtension.ShippingAddressID,
					theNewUserExtension.LastPaymentMethodID, theNewUserExtension.LastAppliedCouponCode, theNewUserExtension.LanguageID, theNewUserExtension.TotalForumPosts, theNewUserExtension.Signature,
					theNewUserExtension.Active, theNewUserExtension.Deleted, theNewUserExtension.RegistrationDate, theNewUserExtension.CurrencyID,
					theNewUserExtension.FirstName, theNewUserExtension.LastName, theNewUserExtension.PhoneNumber, theNewUserExtension.MobileNumber);
			}

			return result;
		}

		public int UpdateUserExtension(Data.Entity.UserExtension theUserExtension)
		{
			if(theUserExtension == null || theUserExtension.UserID == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbUserExtension = (from ue in _dbContext.UserExtensions
									   where ue.UserID == theUserExtension.UserID
									   select ue).FirstOrDefault();

				if(dbUserExtension == null)
				{
					result = 0;
				}
				else
				{
					dbUserExtension.Active = theUserExtension.Active;
					dbUserExtension.AffiliateID = theUserExtension.AffiliateID;
					dbUserExtension.BillingAddressID = theUserExtension.BillingAddressID;
					dbUserExtension.CurrencyID = theUserExtension.CurrencyID;
					dbUserExtension.Deleted = theUserExtension.Deleted;
					dbUserExtension.FirstName = theUserExtension.FirstName;
					dbUserExtension.LanguageID = theUserExtension.LanguageID;
					dbUserExtension.LastAppliedCouponCode = theUserExtension.LastAppliedCouponCode;
					dbUserExtension.LastName = theUserExtension.LastName;
					dbUserExtension.LastPaymentMethodID = theUserExtension.LastPaymentMethodID;
					dbUserExtension.RegistrationDate = theUserExtension.RegistrationDate;
					dbUserExtension.ShippingAddressID = theUserExtension.ShippingAddressID;
					dbUserExtension.Signature = theUserExtension.Signature;
					dbUserExtension.TotalForumPosts = theUserExtension.TotalForumPosts;
					dbUserExtension.UserID = theUserExtension.UserID;
					dbUserExtension.PhoneNumber = theUserExtension.PhoneNumber;
					dbUserExtension.MobileNumber = theUserExtension.MobileNumber;

					_dbContext.SubmitChanges();
					result = 1;
				}
			}

			return result;
		}

		public Data.Entity.UserExtension GetUserExtensionByUserID(System.Guid userID)
		{
			List<ECommerce_GetUserExtension_By_UserIDResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetUserExtension_By_UserID(userID);

				lstData = data.ToList();

			}

			Data.Entity.UserExtension result;

			if(lstData != null && lstData.Count > 0)
			{
				var first = lstData[0];

				result = new Data.Entity.UserExtension()
				{
					Active = first.Active,
					AffiliateID = first.AffiliateID,
					BillingAddressID = first.BillingAddressID,
					CurrencyID = first.CurrencyID,
					Deleted = first.Deleted,
					FirstName = first.FirstName,
					LanguageID = first.LanguageID,
					LastAppliedCouponCode = first.LastAppliedCouponCode,
					LastName = first.LastName,
					LastPaymentMethodID = first.LastPaymentMethodID,
					MobileNumber = first.MobileNumber,
					PhoneNumber = first.PhoneNumber,
					RegistrationDate = first.RegistrationDate,
					ShippingAddressID = first.ShippingAddressID,
					Signature = first.Signature,
					TotalForumPosts = first.TotalForumPosts,
					UserID = first.UserID
				};

				return result;
			}

			return null;
		}

		#endregion
	}
}
